import {BookUpdater} from "../../BookUpdater";
import {requestText} from "../../../function/httpFunc";
import {parseXML, reduceHTML2SimpleXML, StXML} from "../../../function/xmlFunc";
import {joinPath} from "../../../function/fileFunc";
import {MangaSite} from "../MangaSite";

export namespace ListManHuaDui {
    export interface TravelBaseNode extends StXML.BaseNode {
        traveled: boolean;
        children: TravelNode[];
    }
    export interface TravelTextNode extends StXML.TextNode {
        traveled: boolean;
    }
    export type TravelNode = TravelBaseNode | TravelTextNode;
}

export class ListManHuaDui {
    public readonly bookInfo: BookUpdater.BaseInfo;
    public readonly linkPrefix: string = "https://www.manhuadui.com/";
    constructor(bookInfo: BookUpdater.BaseInfo) {
        this.bookInfo = bookInfo;
    }
    public async getNewList(): Promise<BookUpdater.SimpleArticleItem[]> {
        const listURL = this.bookInfo.referURL;
        const html = await requestText(listURL);
        const xml = reduceHTML2SimpleXML(html);
        const xmlObj = parseXML(xml);
        return this.getArticleList(xmlObj);
    }
    public getArticleList(rootNode: StXML.BaseNode) {
        let nowNode: ListManHuaDui.TravelNode = rootNode as ListManHuaDui.TravelBaseNode;
        console.log(rootNode);
        nowNode.traveled = true;
        let targetNode: ListManHuaDui.TravelBaseNode;
        while (true) {
            if (nowNode.isText) {
                const textNode = nowNode as ListManHuaDui.TravelTextNode;
                textNode.traveled = true;
                nowNode = textNode.parent as ListManHuaDui.TravelBaseNode;
            } else {
                const baseNode = nowNode as ListManHuaDui.TravelBaseNode;
                baseNode.traveled = true;
                if (baseNode.tagName.toLowerCase() === "ul" && baseNode.attrMap.id === "chapter-list-1") {
                    targetNode = baseNode;
                    break;
                }
                nowNode = baseNode.children.find((childNode) => !childNode.traveled) as ListManHuaDui.TravelNode;
                if (nowNode == null) {
                    if (baseNode.parent == null) {
                        break;
                    }
                    nowNode = baseNode.parent as ListManHuaDui.TravelNode;
                }
            }
        }
        return this.orgArticleList(targetNode);
    }
    protected orgArticleList(targetNode: ListManHuaDui.TravelBaseNode): BookUpdater.SimpleArticleItem[] {
        const nodeList = targetNode.children as ListManHuaDui.TravelBaseNode[];
        const bookId = this.bookInfo.id;
        return nodeList.map((item, index) => {
            const linkNode = item.children[0] as ListManHuaDui.TravelBaseNode;
            const prefix = this.linkPrefix;
            return {
                article_index: index + 1,
                book_id: bookId,
                download_method: "ManHuaDui",
                localized: 0,
                name: (linkNode.attrMap.title as string),
                origin_url: joinPath(prefix, linkNode.attrMap.href as string),
            };
        });
    }
}
